/*
 *  $Id: IntPhrasePart.cs 1179 2010-11-10 12:29:24Z thenn.erannor $
 *	This file is a part of Ragima CCG.
 *	(C) 2008-2010 Ragima development group.
 *
 *	Licensed under the terms of the GNU GPL License version 2.
 *
 */

namespace Ragima.Languages.PhraseParts {
    /// <summary>
    /// Числовая часть фразы. Может быть знаковым.
    /// </summary>
    public class IntPhrasePart : TypedArgPhrasePart<int> {
        private bool _signed;

        public IntPhrasePart(int argNum, bool signed) : base(argNum) {
            _signed = signed;
        }

        /// <summary>
        /// Строит число как часть фразы.
        /// </summary>
        protected override void BuildArg(PhraseBuilder builder, int arg) {
            if (_signed && arg >= 0)
                builder.Text.Append('+');
            builder.Text.Append(arg);
        }

        /// <summary>
        /// Строит шаблон числовой части фразы.
        /// </summary>
        protected override void BuildTemplateArg(PhraseBuilder builder) {
            builder.Text.Append(PhraseTemplateSymbols.IntCommand);
            if (_signed)
                builder.Text.Append(PhraseTemplateSymbols.IntSignedFormat);
        }

        /// <summary>
        /// Парсер для числовой части фразы.
        /// </summary>
        public sealed class IntParser : IPhrasePartParser {
            public bool NeedArgument {
                get { return true; }
            }

            public bool Test(PhraseTemplateScanner scanner) {
                return scanner.Test(PhraseTemplateSymbols.IntCommand);
            }

            public PhrasePart Create(int argNum, PhraseTemplateScanner scanner) {
                scanner.NextChar();
                return new IntPhrasePart(argNum, scanner.Test(PhraseTemplateSymbols.IntSignedFormat));
            }
        }
    }
}
